﻿using System;

namespace BoilerEvent
{
    class Dog
    {
        private string name;
        private string color;

        public delegate void Handler(); // 定义委托
        public static event Handler NewDog; // 定义事件

        public Dog(string name,string color)
        {
            this.name = name;
            this.color = color;

            if(NewDog !=null)
            {
                Console.WriteLine("新进了一只狗");
                NewDog(); // 调用事件
            }
        }
    }

    class Test
    {
        public static void Main(string[] args)
        {
            Dog d = new Dog("Tony", "yellow"); // 因为还没有添加订阅，所以不能触发事件
            Dog.NewDog += new Dog.Handler(Client1); // Client1添加订阅
            Dog.NewDog += new Dog.Handler(Client2); // Client2添加订阅
            Dog d2 = new Dog("Tom", "white");

            Console.ReadKey();
        }

        static void Client1()
        {
            Console.WriteLine("我喜欢这条狗！");
        }

        static void Client2()
        {
            Console.WriteLine("我非常想要！");
        }
    }
}